package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.entity.OmUserGroup;
import com.augurit.common.system.convert.OmGroupConverter;
import com.augurit.common.system.web.org.form.OmUserGroupForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.CollectionUtils;

/**
 * 用户工作组关联DAO类
 */
@Repository
public class OmUserGroupDao extends BaseDao<OmUserGroup, Long> {

	/**
	 * 保存工作组岗位关联Form对象
	 * @param form 工作组岗位关联Form对象
	 */
	public void save(OmUserGroupForm form){
		
		if(form != null){
			OmUserGroup entity = null;
			
			//准备VO对象
			if(form != null && form.getUgId() != null){
				entity = this.get(form.getUgId());
			}else{
				entity = new OmUserGroup();
			}
			
			//属性值转换
			OmGroupConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setUgId(entity.getUgId());
		}
	}
	
	/**
	 * 根据用户id和工作组id获取实体对象
	 * @param userId
	 * @param groupId
	 * @return
	 */
	public OmUserGroup get(Long userId, Long groupId){
		if(userId != null && groupId != null){
			String hql = "from OmUserGroup ps where ps.userId = ? and ps.groupId = ?";
			List<Long> values = new ArrayList<Long>();
			values.add(userId);
			values.add(groupId);
			List<OmUserGroup> entities = this.find(hql, values);
			if(entities != null && entities.size() == 1){
				return entities.get(0);
			}
		}
		return null;
	}
	/**
	 * 判断用户是否已经存在工作组
	 * @param userId 用户id
	 * @param groupId 工作组id
	 * @return true或false, true表示用户已经属于当前工作组，反之则相反
	 */
	public boolean isUserInGroup(Long userId, Long groupId){
		OmUserGroup entity = this.get(userId, groupId);
		if(entity == null)
			return false;
		return true;
	}
	
	/**
	 * 获取指定工作组下的用户id数组
	 * @param groupId 工作组id
	 * @return 工作组id数组
	 */
	public Long[] getUserIds(Long groupId){
		if(groupId != null){
			String hql = "select ps.userId from OmUserGroup ps where ps.groupId = ?";
			List<Long> values = new ArrayList<Long>();
			values.add(groupId);
			List<Long> list = this.find(hql,values);
			return CollectionUtils.toLongArray(list);
		}
		return null;
	}
	
	/**
	 * 根据用户ID查询关联工作组ID列表
	 * @param userId 用户ID
	 * @return 关联工作组ID列表
	 */
	public List<Long> getGroupIds(Long userId){
		List<Long> list = null;
		if(userId != null){
			list = new ArrayList();
			String hql = "select ps.groupId from OmUserGroup ps where ps.userId= ? ";
			List values = new ArrayList();
			values.add(userId);
			list = this.find(hql,values);
		}
		return list;
	}
	
	/**
	 * 获取一批工作的用户id列表
	 * @param groupIds
	 * @return
	 */
	public Long[] getUserIdsByGroupIds(Long[] groupIds){
		if(groupIds != null && groupIds.length > 0){
			String hql = "select distinct ps.userId from OmUserGroup ps where " + HqlUtils.buildOrCondition("ps", "groupId", groupIds);
			List<Long> list = this.find(hql);
			return CollectionUtils.toLongArray(list);
		}
		return null;
	}
	
	
	/**
	 * 根据工作组id查询关联用户
	 * @param groupId 用户主键
	 * @return 返回跟用户关联的OmUserGroup的主键集合
	 */
	public List<Long> getUgIdsByGroup(Long groupId){
		List<Long> list = null;
		if(groupId != null){
			list = new ArrayList();
			String hql = "select ps.ugId from OmUserGroup ps where ps.groupId= ? ";
			List values = new ArrayList();
			values.add(groupId);
			list = this.find(hql,values);
		}
		return list;
	}
	
	/**
	 * 根据用户ID删除对象
	 * @param userId 用户ID
	 */
	public void deleteByUserId(Long userId){
		if(userId != null){
			List<OmUserGroup> entities = this.findBy("userId", userId);
			if(entities != null && entities.size() > 0)
				this.delete(entities);
		}
	}
	
	
	//根据工作组id删除关联用户
	public void deleteUsersByGroupId(Long groupId){
		if(groupId != null){
			List<Long> list = new ArrayList();
			list = this.getUgIdsByGroup(groupId);
			if(list != null && list.size()!= 0){
				this.delete(CollectionUtils.toLongArray(list));
			}
		}
	}
	
	
	/**
	 * 根据用户id查询关联工作组
	 * @param userId 用户主键
	 * @return 返回跟用户关联的OmUserGroup的主键集合
	 */
	public List<Long> getGroupCascade(Long userId){
		List<Long> list = null;
		if(userId != null){
			list = new ArrayList();
			String hql = "select ps.ugId from OmUserGroup ps where ps.userId= ? ";
			List<Long> values = new ArrayList<Long>();
			values.add(userId);
			list = this.find(hql,values);
		}
		return list;
	}
	
	/**
	 * 根据用户id删除关联工作组
	 * @param userId 用户主键
	 */
	public void deleteGroupCascade(Long userId){
		if(userId != null){
			List<Long> list = new ArrayList();
			list = this.getGroupCascade(userId);
			if(list != null && list.size()!= 0){
				Long ids[] = new Long[list.size()];
				ids = list.toArray(ids);
				this.delete(ids);
			}
		}
	}
	
	/**
	 * 获取工作组下的直属用户id数组
	 * @param groupId 工作组id
	 * @return 用户id数组
	 */
	@Transactional(readOnly = true)
	public Long[] getUserIdsByGroupId(Long groupId){
		if(groupId != null){
			String hql = "select ps.userId from OmUserGroup ps where ps.groupId=?" ;
			List values = new ArrayList();
			values.add(groupId);
			
			List<Long> list = this.find(hql, values);
			return CollectionUtils.toLongArray(list);
		}
		return null;
	}
	
	/**
	 * 删除用户与工作组的关联，不删除用户本身
	 * @param userId 用户主键
	 * @param groupId 工作组主键
	 */
	public void deleteUserFromGroup(Long userId, Long groupId){
		// TODO 删除用户关联的工作组
		if(userId != null && groupId != null){
			String hql = "from OmUserGroup ps where userId = ? and groupId = ?";
			List<Long> values = new ArrayList<Long>();
			values.add(userId);
			values.add(groupId);
			
			OmUserGroup entity = (OmUserGroup)this.findUnique(hql, values);
			this.delete(entity);
		}
	}
}
